/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.IO;

namespace ArtNotes
{
	[Register ("AppDelegate")]
	public partial class AppDelegate : UIApplicationDelegate
	{
		UIWindow window;
		ANTabBarController tabBarController;
		
		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
		{
			NSUserDefaults prefs = NSUserDefaults.StandardUserDefaults;
			
			bool geoTaggingEnabled = true;
			bool initialSetupEnabled = true;
			bool advancedEnabled = false;
			
			// Hack to work around the problem that the settings aren't installed
			// until the first run of the application. Thus, checking to see if the
			// initial setup routine should be run results in it being skipped.
			// Smooth. Maybe if I think of an idea like that Apple will employ me.
			if(Directory.Exists(AN_APP_OPTIONS.DOC_DIR))
			{
				geoTaggingEnabled = prefs.BoolForKey("an_geotag");
				advancedEnabled = prefs.BoolForKey("an_adv");
				initialSetupEnabled = prefs.BoolForKey("an_initsetup");
				prefs.SetBool(false,"an_initsetup");
			}
			else
			{
				prefs.SetBool(true,"an_geotag");
				prefs.SetBool(false,"an_adv");
			}
			
			this.initApp (geoTaggingEnabled,initialSetupEnabled,advancedEnabled);
			
			window.AddSubview (tabBarController.View);
			
			window.MakeKeyAndVisible ();

			return true;
		}
		
		public override void OnActivated (UIApplication application)
		{
		}
		
		/// <summary>
		/// Initialiases the application, setting up globally
		/// accessable 'variables' such as the GPS and
		/// access to the database.
		/// Also configures access to Flickr using the
		/// API key in AN_APP_OPTIONS.
		/// </summary>
		/// <param name="geoTaggingEnabled">
		/// Enables / disables the GPS
		/// </param>
		/// <param name="initialSetupEnabled">
		/// If this is set to true it will kick start the
		/// intial application set-up, removing and
		/// recreating the database and all images associated
		/// </param>
		/// <param name="advancedEnabled">
		/// Disabled in current version
		/// </param>
		public void initApp (bool geoTaggingEnabled, bool initialSetupEnabled, bool advancedEnabled)
		{
			ANSetup anSetup = new ANSetup();
			
			if(geoTaggingEnabled)
			{
				Console.WriteLine("Geo-Tagging Enabled");
				AN_APP_OPTIONS.AN_LOCATION = anSetup.startGPS();
			}
			else
			{
				Console.WriteLine("Geo-Tagging Disabled");
				AN_APP_OPTIONS.AN_LOCATION = null;
			}
			
			if(initialSetupEnabled)
			{
				Console.WriteLine("Running Initial Setup");
				AN_APP_OPTIONS.AN_DATABASE = anSetup.initialDBSetup();
			}
			else
			{
				Console.WriteLine("Using Existing Database");
				AN_APP_OPTIONS.AN_DATABASE = anSetup.existingDBSetup();
			}
			
			if(advancedEnabled)
			{
				Console.WriteLine("Advanced Editing Enabled");
				AN_APP_OPTIONS.AN_ADVANCED = true;
			}
			else
			{
				Console.WriteLine("Advanced Editing Disabled");
				AN_APP_OPTIONS.AN_ADVANCED = false;
			}
			
			AN_APP_OPTIONS.AN_FLICKR = anSetup.intiFlickr();
			
			window = new UIWindow (UIScreen.MainScreen.Bounds);
			tabBarController = new ANTabBarController ();
			
		}
	}
}
